/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

function tabletAppItemPositionChanged(mouseArea, pageChangeTimer, toDesktopTimer, appIconItem, appIconItemRoot,
                                popupGroupContainer, popupGroupContainerId) {
    if (mouseArea.Drag.active && !mouseArea.groupHeightChanged &&
            (appIconItemRoot.x - mouseArea.startDragPoint.x > 0
             || appIconItemRoot.y - mouseArea.startDragPoint.y > 0)) {
        changeGroupHeight()
        mouseArea.groupHeightChanged = true
    }

    if (allowGroupItemToTaskbar) {
        appIconItem.iconWidth = 64 * Style.tabletScaleFactor
    }

    if (!allowGroupItemToTaskbar && itemRoot.Drag.active) {
        appIconItem.iconWidth = 96 * Style.tabletScaleFactor * 1.1
    }
    if (!GroupController.groupPopuped) {
        return
    }

    var itemPos = appIconItem.mapToItem(popupGroupContainer, 0, 0)
    if (itemPos.x % popupGroupContainer.width < -appIconItem.width ||
                itemPos.x > popupGroupContainer.width ||
                itemPos.y % popupGroupContainer.height < -appIconItem.height ||
                itemPos.y > popupGroupContainer.height) {
        pageChangeTimer.stop()
        mouseArea.isChangeListviewPage = false
        if (GroupController.groupPopuped) {
            toDesktopTimer.start()
        }
    } else if ((itemPos.x % popupGroupContainer.width) >= (popupGroupContainer.width - appIconItem.width + 32 * Style.tabletScaleFactor) &&
            mouseArea.Drag.active) {
        pageChangeTimer.start()
        toDesktopTimer.stop()
        if (mouseArea.isChangeListviewPage) {
            if (popupGroupContainer.currentIndex < popupGroupContainer.count - 1) {
                popupGroupContainer.currentIndex ++
                modelManager.removePlaceholderItem()
                // 拖动至不同页时才添加 placeholder item
                if ((mouseArea.drag.target.groupId === popupGroupContainerId &&
                     mouseArea.drag.target.pageIndex !== popupGroupContainer.currentIndex) ||
                        mouseArea.drag.target.groupId !== popupGroupContainerId)  {
                    if (popupGroupContainer.currentIndex === -1) {
                        return
                    }
                    var groupCurItemCount = modelManager.groupPageItemCount(popupGroupContainerId, popupGroupContainer.currentIndex)
                    var index = groupCurItemCount > 0 ? groupCurItemCount : 0
                    modelManager.addPlaceholderItem(index, popupGroupContainer.currentIndex, mouseArea.drag.target.rowSpan,
                                mouseArea.drag.target.columnSpan, popupGroupContainerId)
                }
                mouseArea.isChangeListviewPage = false
                pageChangeTimer.restart()
            } else {
                appIconItem.x = 0
            }
        }
    } else if ((itemPos.x % popupGroupContainer.width < -32 * Style.tabletScaleFactor) &&
               mouseArea.Drag.active) {
        pageChangeTimer.start()
        toDesktopTimer.stop()
        if (mouseArea.isChangeListviewPage && popupGroupContainer.currentIndex > 0) {
            popupGroupContainer.currentIndex --
            modelManager.removePlaceholderItem()
            if ((mouseArea.drag.target.groupId === popupGroupContainerId &&
                 mouseArea.drag.target.pageIndex !== popupGroupContainer.currentIndex) ||
                    mouseArea.drag.target.groupId !== popupGroupContainerId)  {
                if (popupGroupContainer.currentIndex === -1) {
                    return
                }
                groupCurItemCount = modelManager.groupPageItemCount(popupGroupContainerId, popupGroupContainer.currentIndex)
                index = groupCurItemCount > 0 ? groupCurItemCount : 0
                modelManager.addPlaceholderItem(index, popupGroupContainer.currentIndex, mouseArea.drag.target.rowSpan,
                            mouseArea.drag.target.columnSpan, popupGroupContainerId)

            }
            mouseArea.isChangeListviewPage = false
            pageChangeTimer.restart()
        }
    } else {
        pageChangeTimer.stop()
        toDesktopTimer.stop()
        mouseArea.isChangeListviewPage = false
    }
}

function eduAppItemPositionChanged(dragMousearea, item, listView, scrollingTimer, groupId) {
    var itemPos = item.mapToItem(listView, 0, 0)
    if ((itemPos.x % listView.width) >= (listView.width - item.width/2) &&
            dragMousearea.Drag.active && !dragMousearea.isScrolling ) {
        if (listView.currentIndex < listView.count - 1) {
            listView.currentIndex ++
            dragMousearea.isScrolling = true
            scrollingTimer.start()
            modelManager.removePlaceholderItem()
            // 拖动至不同页时才添加 placeholder item
            if (dragMousearea.drag.target.pageIndex !== listView.currentIndex) {
                var index = modelManager.groupPageItemCount(groupId, listView.currentIndex) > 0 ?
                            modelManager.groupPageItemCount(groupId, listView.currentIndex) - 1 : 0
                modelManager.addPlaceholderItem(index, listView.currentIndex, dragMousearea.drag.target.rowSpan,
                            dragMousearea.drag.target.columnSpan, groupId)
            }
        } else {
            item.x = 0
        }
    } else if ((itemPos.x % listView.width < -item.width/2) &&
               dragMousearea.Drag.active && !dragMousearea.isScrolling) {
        if (listView.currentIndex > 0) {
            listView.currentIndex --
            dragMousearea.isScrolling = true
            scrollingTimer.start()
            modelManager.removePlaceholderItem()
            if (dragMousearea.drag.target.pageIndex !== listView.currentIndex) {
                index = modelManager.groupPageItemCount(groupId, listView.currentIndex) > 0 ?
                        modelManager.groupPageItemCount(groupId, listView.currentIndex) - 1 : 0
                modelManager.addPlaceholderItem(index, listView.currentIndex, dragMousearea.drag.target.rowSpan,
                            dragMousearea.drag.target.columnSpan, groupId)
            }
        }
    }
}

function tabletLauncherItemPositionChanged(item, listView, moveDropArea,
                                           dragMousearea, scrollingTimer) {
    var itemPos = item.mapToItem(listView, 0, 0)
    if ((itemPos.x % listView.width) >= (listView.width - (item.width / 2 + moveDropArea.width / 2) * Style.tabletScaleFactor) &&
            dragMousearea.Drag.active && !dragMousearea.isScrolling && !GroupController.groupPopuped) {
        if (listView.currentIndex < listView.count - 1) {
            listView.currentIndex ++
            dragMousearea.isScrolling = true
            scrollingTimer.start()
            modelManager.removePlaceholderItem()
            // 拖动至不同页时才添加 placeholder item
            if (dragMousearea.drag.target.pageIndex !== listView.currentIndex) {
                var curPageItemCount = modelManager.groupPageItemCount(-1, listView.currentIndex);
                var index = curPageItemCount > 0 ? curPageItemCount : 0
                modelManager.addPlaceholderItem(index, listView.currentIndex, dragMousearea.drag.target.rowSpan,
                            dragMousearea.drag.target.columnSpan)
            }
        } else {
            item.x = 0
        }
    } else if ((itemPos.x % listView.width < -item.width/2 + moveDropArea.width / 2 * Style.tabletScaleFactor) &&
               dragMousearea.Drag.active && !dragMousearea.isScrolling && !GroupController.groupPopuped) {
        if (listView.currentIndex > 0) {
            listView.currentIndex --
            dragMousearea.isScrolling = true
            scrollingTimer.start()
            modelManager.removePlaceholderItem()
            if (dragMousearea.drag.target.pageIndex !== listView.currentIndex) {
                curPageItemCount = modelManager.groupPageItemCount(-1, listView.currentIndex);
                index = curPageItemCount > 0 ? curPageItemCount : 0
                modelManager.addPlaceholderItem(index, listView.currentIndex, dragMousearea.drag.target.rowSpan,
                            dragMousearea.drag.target.columnSpan, groupId)
            }
        }
    }

}
